package com.yz.platform.modules.biz.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yz.platform.common.utils.Constant;
import com.yz.platform.common.utils.UUIDHelper;
import com.yz.platform.modules.biz.dao.ActivationCodeDao;
import com.yz.platform.modules.biz.dao.ScriptDao;
import com.yz.platform.modules.biz.entity.ActivationCodeEntity;
import com.yz.platform.modules.biz.entity.ScriptEntity;
import com.yz.platform.modules.biz.service.ActivationCodeService;
import com.yz.platform.modules.sys.dao.SysConfigDao;
import com.yz.platform.modules.sys.dao.SysUserDao;
import com.yz.platform.modules.sys.entity.SysConfigEntity;
import com.yz.platform.modules.sys.entity.SysUserEntity;

@Transactional(readOnly = true)
@Service("activationCodeService")
public class ActivationCodeServiceImpl implements ActivationCodeService {

    @Autowired
    private ActivationCodeDao activationCodeDao;

    @Autowired
    private SysConfigDao sysConfigDao;

    @Autowired
    private ScriptDao scriptDao;

    @Autowired
    private SysUserDao userDao;

    @Override
    public ActivationCodeEntity queryObject(Integer codeId) {
        return activationCodeDao.queryObject(codeId);
    }

    @Override
    public ActivationCodeEntity getByCode(String code) {
        return activationCodeDao.getByCode(code);
    }

    @Override
    public List<ActivationCodeEntity> queryList(Map<String, Object> map) {

        List<ActivationCodeEntity> queryResult = activationCodeDao.queryList(map);

        //更新用户名
        List<ActivationCodeEntity> newResult = Lists.newArrayList();

        for (ActivationCodeEntity codeEntity : queryResult) {

            SysUserEntity user = userDao.queryObject(codeEntity.getUserId());
            if (user != null) {
                codeEntity.setUserName(user.getUsername());
            }
            SysUserEntity createUser = userDao.queryObject(codeEntity.getCreateUser());
            if (createUser != null) {
                codeEntity.setCreateUserName(createUser.getUsername());
            }

            ScriptEntity scriptEntity = scriptDao.queryObject(codeEntity.getScriptId());

            if (scriptEntity != null) {
                codeEntity.setScriptName(scriptEntity.getScriptName());
            }

            newResult.add(codeEntity);

        }
        return newResult;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return activationCodeDao.queryTotal(map);
    }

    /**
     * 保存激活码
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public void save(ActivationCodeEntity activationCode) {

        //默认每个脚本对应5个
        int codeCount = 5;

        SysConfigEntity configEntity = sysConfigDao.queryByKey(Constant.DICT_CODE_COUNT);
        if (configEntity != null) {
            codeCount = configEntity.getValue() == null ? 5 : Integer.parseInt(configEntity.getValue());
        }

        for (int i = 0; i < activationCode.getScriptArray().length; i++) {

            for (int j = 0; j < codeCount; j++) {

                if (Strings.isNullOrEmpty(activationCode.getScriptArray()[i])) continue;

                ActivationCodeEntity codeEntity = new ActivationCodeEntity();
                codeEntity.setCodeNo(UUIDHelper.getId());
                codeEntity.setUserId(activationCode.getUserId());
                codeEntity.setCreateTime(new Date());
                codeEntity.setCreateUser(activationCode.getCreateUser());
                codeEntity.setScriptId(Integer.parseInt(activationCode.getScriptArray()[i]));
                codeEntity.setStatus(0);
                codeEntity.setExpires(activationCode.getExpires());

                activationCodeDao.save(codeEntity);

            }
        }

    }

    @Transactional(readOnly = false)
    @Override
    public void update(ActivationCodeEntity activationCode) {
        activationCodeDao.update(activationCode);
    }

    @Transactional(readOnly = false)
    @Override
    public void delete(Integer codeId) {
        activationCodeDao.delete(codeId);
    }

    @Transactional(readOnly = false)
    @Override
    public void deleteBatch(Integer[] codeIds) {
        activationCodeDao.deleteBatch(codeIds);
    }


    @Transactional(readOnly = false)
    @Override
    public void stopBatch(Integer[] codeIds) {
        activationCodeDao.stopBatch(codeIds);
    }

}
